#!/bin/sh

base=${0%/*}; test -z "$base" && base=.

cfglog="config.log.$$"
true > "$cfglog"
newcfglog() { mv "$cfglog" "$1"; cfglog="$1"; }
appendlog() { cat "$cfglog" >> "$1"; rm -f "$cfglog"; cfglog="$1"; }

config="config.sh.$$"
echo "#!/bin/sh" > $config
echo "" >> $config
setconfig() { mv "$config" "$1"; config="$1"; }
delconfig() { rm "$config"; }

. $base/configure__f.sh
. $base/configure_args.sh

if [ "$mode" = 'help' ]; then
	. $base/configure_help.sh
	exit 0
fi

if [ -z "$keeplog" -a "$mode" != 'regen' ]; then
	newcfglog 'config.log'
else
	appendlog 'config.log'
fi

# Possible modes are: (none), native, cross, buildmini, target
# Default mode, (none), means -z $mode -a -z $target
# $build is needed for (none), native, buildmini, and for cross to pass it to buildmini
# $target is needed for cross and target

if [ -z "$buildarch" -a "$mode" != "target" ]; then
	mstart "Guessing build platform"
	if [ "x$build" = "x" ]; then
		buildarch=`$base/config.guess 2>>$cfglog`
	else
		buildarch=`$base/config.sub "$build" 2>>$cfglog`
	fi
	if [ -n "$buildarch" ]; then
		result "$buildarch"
	else
		die "cannot determine build platform"
	fi
fi
# past this point $build/$buildarch are non-empty unless we're in target mode

if [ -z "$mode" ]; then
	if [ -z "$target" -o "$target" = "$build" ]; then
		mode='native'
	else
		mode='cross'
	fi
fi
# past this point $mode can't be empty

if [ "$mode" = "buildmini" -o "$mode" = "native" ]; then
	targetarch="$buildarch"
	target="$build"
fi

if [ -n "$target" -a -z "$targetarch" ]; then
	mstart "Guessing target architecture"	
	targetarch=`$base/config.sub "$target" 2>>$cfglog`
	if [ -n "$targetarch" ]; then
		result "$targetarch"
	else
		die "cannot determine target platform"
	fi
elif [ -z "$target" ]; then
	if [ "$mode" = 'cross' -o "$mode" = 'target' ]; then
		die "--target must be specified for a cross build"
	fi
fi

if [ "$mode" = "cross" ]; then
	delconfig
	msg "Respawning configure for build and target platforms"
	msg
	run $0 $hco --keeplog --mode=buildmini --build="$build" --buildarch="$buildarch"\
		|| die "configure --mode=buildmini failed"
	msg
       	run $0 "$@" --keeplog --mode=target --target="$target" --targetarch="$targetarch"\
		|| die "configure --mode=target failed"
	msg
	msg "Ok, back to the top-level configure"
elif [ "$mode" = "buildmini" ]; then
	setconfig "xconfig.sh"
	targetlabel=`archlabel "$build" "$targetarch"`
	msg "Configuring build-time miniperl for $targetlabel"
elif [ "$mode" = "target" ]; then
	setconfig "config.sh"
	targetlabel=`archlabel "$target" "$targetarch"`
	msg "Configuring primary perl executable for $targetlabel"
elif [ "$mode" = "native" ]; then 
	setconfig "config.sh"
	msg "Configuring for a native build"
fi


if [ "$mode" = 'native' -o "$mode" = 'buildmini' -o "$mode" = "target" ]; then
	test "$mode" = "target" && define usecrosscompile 'define'
	define targetarch "$targetarch"

	. $base/configure_version.sh
	. $base/configure_tool.sh
	. $base/configure_hint.sh
	. $base/configure_hdrs.sh
	. $base/configure_type.sh
	. $base/configure_type_ext.sh
	. $base/configure_type_sel.sh
	. $base/configure_type_dbl.sh
	. $base/configure_sigs.sh
	. $base/configure_libs.sh
	. $base/configure_func.sh
	. $base/configure_func_dbl.sh
	. $base/configure_func_def.sh
	. $base/configure_func_ext.sh
	. $base/configure_func_sel.sh
	. $base/configure_attr.sh
	. $base/configure_path.sh
	. $base/configure_thrd.sh
	. $base/configure_pfmt.sh
	. $base/configure_mods.sh
	. $base/configure_misc.sh
fi

test -f ./config_h.SH || die "Not in perl base directory, aborting"

if [ "$mode" = 'native' ]; then
	echo "Generating config.h"
	./config_h.SH
	echo "Generating Makefile.config"
	./Makefile.config.SH

	echo
	echo "Configuration completed for native build"
	echo
	echo "            platform:  $archname"
	echo "          c compiler:  $cc"
	echo "                  ld:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo
	echo "              prefix:  $prefix"
	echo " installation prefix:  $installprefix"
	echo "         executables:  $bin"
	echo "             scripts:  $bin"
	echo "             privlib:  $privlib"
	echo "             archlib:  $archlib"
	echo " section 1 man pages:  $man1dir  (with '$man1ext' extension)"
	echo " section 3 man pages:  $man3dir  (with '$man3ext' extension)"
	echo
elif [ "$mode" = 'cross' ]; then
	echo "Generating config.h and xconfig.h"
	CONFIG_SH=config.sh  CONFIG_H=config.h  ./config_h.SH
	CONFIG_SH=xconfig.sh CONFIG_H=xconfig.h ./config_h.SH
	echo "Generating Makefile.config"
	./Makefile.config.SH
	echo
	echo "Configuration completed for cross build"
	echo
	. ./xconfig.sh
	echo "      build platform:  $archname"
	echo "          C compiler:  $cc"
	echo "              linker:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo "             objdump:  $objdump"
	echo
	. ./config.sh
	echo "     target platform:  $archname"
	echo "          C compiler:  $cc"
	echo "                  ld:  $ld"
	echo "                  ar:  $ar"
	echo "              ranlib:  $ranlib"
	echo "             objdump:  $objdump"
	echo
	echo "              prefix:  $prefix"
	echo "             sysroot:  $sysroot"
	echo " installation prefix:  $installprefix"
	echo "         executables:  $bin"
	echo "             scripts:  $bin"
	echo "             privlib:  $privlib"
	echo "             archlib:  $archlib"
	echo " section 1 man pages:  $man1dir  (with '$man1ext' extension)"
	echo " section 3 man pages:  $man3dir  (with '$man3ext' extension)"
	echo
elif [ "$mode" = "regen" ]; then
	if [ -f config.sh ]; then
		CONFIG_SH=config.sh  CONFIG_H=config.h  ./config_h.SH
	fi
	if [ -f xconfig.sh ]; then
		CONFIG_SH=xconfig.sh CONFIG_H=xconfig.h ./config_h.SH
	fi
	echo "Generating Makefile.config"
	./Makefile.config.SH
fi
